package com.evenvi.bio;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TimeClient {

    private String host;
    private int port;

    public TimeClient(String host, int port){
        this.host = host;
        this.port = port;
    }

    public void start(){
        Socket socket = null;
        BufferedReader  in = null;
        PrintWriter out = null;
        try {
            socket = new Socket(this.host, this.port);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            out.println("QUERY TIME ORDER");
            System.out.println("Send order to server succeed.");
            String resp = in.readLine();
            System.out.println("Now is : " + resp);
        }catch (Exception e){

        }finally {
            if(out != null){
                out.close();
                out = null;
            }

            if(in != null){
                try {
                    in.close();
                }catch (IOException el){
                    el.printStackTrace();
                }
            }

            if(socket != null){
                try {
                    socket.close();
                }catch (Exception el){
                    el.printStackTrace();
                }

                socket = null;
            }
        }
    }

    public static void main(String[] args) {
        if(args.length != 2)
        {
            System.out.println("Sage: " + TimeClient.class.getSimpleName() + " <host> <port>");
            return;
        }

        String host = args[0];
        int port = Integer.valueOf(args[1]);
        new TimeClient(host, port).start();
    }
}
